package com.example.yoho.mvp.presenter;

import android.util.Log;

import com.example.lib_core.base.BaseEntity;
import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.BrandContract;
import com.example.yoho.mvp.model.entity.BrandGoodsEntity;
import com.example.yoho.mvp.model.entity.BrandListEntity;
import com.example.yoho.mvp.model.entity.BrandTabEntity;
import com.example.yoho.mvp.model.entity.MenuEntity;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BrandPresenter extends BasePresenter<BrandContract.Model,BrandContract.View> {

    private int page =0;
    private int menuId =1;
    private int categoryId =1;

    @Inject
    public BrandPresenter() {
    }

    private static final String TAG = "BrandPresenter";
    public void BrandRequestAll(int menuId,int categoryId){

        this.menuId = menuId;
        this.categoryId = categoryId;
        page=0;
        JSONObject brandTabJob = new JSONObject();
        JSONObject brandGoodsJob = new JSONObject();
        JSONObject brandListJob = new JSONObject();
        try {
            brandTabJob.put("menuid","1");
            brandGoodsJob.put("categoryid",""+categoryId);
            brandListJob.put("menuid","1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Observable<BaseEntity> observable=Observable.merge(mModel.menuModel(),
                mModel.brandTabModel(brandTabJob.toString())
                ,mModel.brandGoods(brandGoodsJob.toString())
                ,mModel.brandList(brandListJob.toString()));
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposables.add(d);
                mView.showDialog();
            }

            @Override
            public void onNext(BaseEntity baseEntity) {
                if (baseEntity instanceof MenuEntity){
                    Log.i("wym", "onNext:123123123123123123123123123+1 ");
                    MenuEntity menuEntity= (MenuEntity) baseEntity;
                    mView.showMenu(menuEntity);
                }else if (baseEntity instanceof BrandTabEntity){
                    Log.i("wym", "onNext:123123123123123123123123123+2 ");
                    BrandTabEntity brandTabEntity= (BrandTabEntity) baseEntity;
                    mView.showBrandTab(brandTabEntity.getValues());
                }else if (baseEntity instanceof BrandGoodsEntity){
                    Log.i("wym", "onNext:123123123123123123123123123+3 ");
                    BrandGoodsEntity brandGoodsEntity= (BrandGoodsEntity) baseEntity;
                    mView.showBrandGoods(brandGoodsEntity.getValues());
                }else if (baseEntity instanceof BrandListEntity){
                    BrandListEntity brandListEntity= (BrandListEntity) baseEntity;
                    mView.showBrandList(brandListEntity);
                }
                Log.e("ysk", "onNext: "+baseEntity.status+"=="+baseEntity.msg );
            }

            @Override
            public void onError(Throwable e) {
                mView.showToast("请求出错"+e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.hideDialog();
            }
        });

    }

}
